package com.baike.service.impl;

import com.baike.entity.Menu;
import com.baike.entity.User;
import com.baike.mapper.MenuMapper;
import com.baike.mapper.RoleMenuMapper;
import com.baike.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author x_FG
 * @since 2021-10-28
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public Menu getMenuById(Integer id) {
        return menuMapper.selectById(id);
    }

    @Override
    public boolean deleteMenuInfo(Integer id) {
        return menuMapper.deleteById(id)>0;
    }

    @Override
    public boolean updateMenuInfo(Menu menu) {
        return menuMapper.updateById(menu)>0;
    }

    @Override
    public boolean insertMenuInfo(Menu menu) {
        return menuMapper.insert(menu)>0;
    }

    // 查询perms  防止别的用户拿到连接调用
    @Override
    public List<String> selectMenuPerms(Integer uid) {
        return menuMapper.selectMenuPerms(uid);
    }

    // menu
    @Override
    public List<Map<String, Object>> getMenuZtree() {
        List<Menu> menuList = menuMapper.selectList(null);
        return getMenuInfos(menuList,0);
    }
    public List<Map<String,Object>> getMenuInfoZtree(List<Menu> menuList,Integer pid){
        List<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0; i < menuList.size(); i++) {
            Map<String,Object> map = null;
            Menu menu = menuList.get(i);
            if (menu.getPid() == pid){
                map = new HashMap<>();
                map.put("id",menu.getId());
                map.put("name",menu.getText());
                map.put("children",getMenuInfoZtree(menuList,menu.getId()));
            }
            if (map!=null){
                list.add(map);
                List<Map<String,Object>> nodes = (List<Map<String, Object>>) map.get("children");
                if (nodes.size()<=0){
                    map.remove("children");
                }else {
                    map.put("open",true);
                }
            }
        }
        return list;
    }

    // treeView
    @Override
    public List<Map<String, Object>> getMenuInfo() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
//        List<Menu> menuList = menuMapper.selectList(null);
        List<Menu> menuList = menuMapper.selectUserRoleMenu(user.getUid());
        return getMenuInfos(menuList,0);
    }
    public List<Map<String,Object>> getMenuInfos(List<Menu> menuList,Integer pid){
        List<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0; i < menuList.size(); i++) {
            Map<String,Object> map = null;
            Menu menu = menuList.get(i);
            if (menu.getPid() == pid){
                map = new HashMap<>();
                map.put("id",menu.getId());
                map.put("text",menu.getText());
                map.put("url",menu.getUrl());
                map.put("nodes",getMenuInfos(menuList,menu.getId()));
            }
            if (map!=null){
                list.add(map);
                List<Map<String,Object>> nodes = (List<Map<String, Object>>) map.get("nodes");
                if (nodes.size()<=0){
                    map.remove("nodes");
                }else {
                    map.put("open",true);
                }
            }
        }
        return list;
    }

    // 角色权限调用修改方法
    @Override
    public List<Map<String, Object>> getRoleMenu(Integer rid) {
        List<Menu> menuList = menuMapper.selectList(null);
        List<Integer> MenuIds = roleMenuMapper.getRoleMenuByMenuId(rid);
        return getMenuZtreeCheck(menuList,0,MenuIds);
    }
    public List<Map<String,Object>> getMenuZtreeCheck(List<Menu> menuList,Integer pid,List<Integer> mids){
        List<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0; i < menuList.size(); i++) {
            Map<String,Object> map = null;
            Menu menu = menuList.get(i);
            if (menu.getPid() == pid){
                map = new HashMap<>();
                map.put("id",menu.getId());
                map.put("name",menu.getText());
                if (mids.contains(menu.getId())){
                    map.put("checked",true);
                }
                map.put("children",getMenuZtreeCheck(menuList,menu.getId(),mids));
            }
            if (map!=null){
                list.add(map);
                List<Map<String,Object>> nodes = (List<Map<String, Object>>) map.get("children");
                if (nodes.size()<=0){
                    map.remove("children");
                }else {
                    map.put("open",true);
                }
            }
        }
        return list;
    }
}
